﻿/*
 * Almathy Center - Vcard.cs
 * Copyright (C) 2011 Christophe LEMOINE
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * */
using System;
using System.Collections.Generic;

namespace AlmathyCenter.Datas
{

    public class Vcard
    {

        public delegate void RetreiveHandler(agsXMPP.Jid jid);
        public event RetreiveHandler Retreived;

        private agsXMPP.XmppClientConnection xmpp = null;
        private agsXMPP.Jid jid = null;

        public Vcard(agsXMPP.XmppClientConnection xmppc)
        {
            xmpp = xmppc;
        }

        public void RetreiveFrom(agsXMPP.Jid jidc)
        {
            jid = jidc;
            agsXMPP.protocol.iq.vcard.VcardIq viq = new agsXMPP.protocol.iq.vcard.VcardIq();
            viq.Type = agsXMPP.protocol.client.IqType.get;
            viq.From = xmpp.MyJID;
            viq.To = jid;
            xmpp.IqGrabber.SendIq(viq, new agsXMPP.IqCB(vcardIqResult), viq.Id);
        }

        public agsXMPP.protocol.iq.vcard.Vcard toVcard()
        {
            agsXMPP.protocol.iq.vcard.Vcard vcard = new agsXMPP.protocol.iq.vcard.Vcard();
            vcard.Nickname = Nickname;
            vcard.Fullname = Fullname;
            vcard.Birthday = Birthday;
            vcard.Description = Description;
            if(Name != null)
                vcard.Name = new agsXMPP.protocol.iq.vcard.Name(Name.Family.Trim(), Name.Given.Trim(), Name.Middle.Trim());

            if(Organization != null)
                vcard.Organization = new agsXMPP.protocol.iq.vcard.Organization(Organization.Name.Trim(), Organization.Unit.Trim());

            vcard.Role = Role;
            vcard.Title = Title;
            vcard.Url = Url;
            if(HomeEmail != null)
                vcard.AddEmailAddress(HomeEmail);

            if (WorkEmail != null)
                vcard.AddEmailAddress(WorkEmail);

            if (HomePhones != null)
            {
                foreach (KeyValuePair<agsXMPP.protocol.iq.vcard.TelephoneType, agsXMPP.protocol.iq.vcard.Telephone> p in HomePhones)
                    vcard.AddTelephoneNumber(p.Value);
            }

            if (WorkPhones != null)
            {
                foreach (KeyValuePair<agsXMPP.protocol.iq.vcard.TelephoneType, agsXMPP.protocol.iq.vcard.Telephone> p in WorkPhones)
                    vcard.AddTelephoneNumber(p.Value);
            }

            if (HomeAddress != null)
                vcard.AddAddress(HomeAddress);

            if (WorkAddress != null)
                vcard.AddAddress(WorkAddress);

            vcard.Photo = Photo;
            return vcard;
        }

        private void vcardIqResult(object sender, agsXMPP.protocol.client.IQ iq, object data)
        {
            string iqID = data as string;

            if (iq.Type != agsXMPP.protocol.client.IqType.error)
            {
                if (iq.Type == agsXMPP.protocol.client.IqType.result && iq.Vcard != null)
                {
                    _Nickname = (iq.Vcard.Nickname != null) ? iq.Vcard.Nickname.Trim() : string.Empty;
                    _Fullname = (iq.Vcard.Fullname != null) ? iq.Vcard.Fullname.Trim() : string.Empty;
                    _Birthday = (iq.Vcard.Birthday != null) ? iq.Vcard.Birthday : DateTime.Now;
                    _Description = (iq.Vcard.Description != null) ? iq.Vcard.Description.Trim() : string.Empty;
                    _Name.Family = (iq.Vcard.Name != null && iq.Vcard.Name.Family != null) ? iq.Vcard.Name.Family.Trim() : string.Empty;
                    _Name.Given = (iq.Vcard.Name != null && iq.Vcard.Name.Given != null) ? iq.Vcard.Name.Given.Trim() : string.Empty;
                    _Name.Middle = (iq.Vcard.Name != null && iq.Vcard.Name.Middle != null) ? iq.Vcard.Name.Middle.Trim() : string.Empty;
                    _Organization.Name = (iq.Vcard.Organization != null && iq.Vcard.Organization.Name != null) ? iq.Vcard.Organization.Name.Trim() : string.Empty;
                    _Organization.Unit = (iq.Vcard.Organization != null && iq.Vcard.Organization.Unit != null) ? iq.Vcard.Organization.Unit.Trim() : string.Empty;
                    _Role = (iq.Vcard.Role != null) ? iq.Vcard.Role.Trim() : string.Empty;
                    _Title = (iq.Vcard.Title != null) ? iq.Vcard.Title.Trim() : string.Empty;
                    _Url = (iq.Vcard.Url != null) ? iq.Vcard.Url.Trim() : string.Empty;
                    _HomeEmail = iq.Vcard.GetEmailAddress(agsXMPP.protocol.iq.vcard.EmailType.HOME);
                    _WorkEmail = iq.Vcard.GetEmailAddress(agsXMPP.protocol.iq.vcard.EmailType.WORK);
                    _HomePhones[agsXMPP.protocol.iq.vcard.TelephoneType.NUMBER] = iq.Vcard.GetTelephoneNumber(agsXMPP.protocol.iq.vcard.TelephoneType.NUMBER, agsXMPP.protocol.iq.vcard.TelephoneLocation.HOME);
                    _HomePhones[agsXMPP.protocol.iq.vcard.TelephoneType.FAX] = iq.Vcard.GetTelephoneNumber(agsXMPP.protocol.iq.vcard.TelephoneType.FAX, agsXMPP.protocol.iq.vcard.TelephoneLocation.HOME);
                    _HomePhones[agsXMPP.protocol.iq.vcard.TelephoneType.MSG] = iq.Vcard.GetTelephoneNumber(agsXMPP.protocol.iq.vcard.TelephoneType.MSG, agsXMPP.protocol.iq.vcard.TelephoneLocation.HOME);
                    _WorkPhones[agsXMPP.protocol.iq.vcard.TelephoneType.NUMBER] = iq.Vcard.GetTelephoneNumber(agsXMPP.protocol.iq.vcard.TelephoneType.NUMBER, agsXMPP.protocol.iq.vcard.TelephoneLocation.WORK);
                    _WorkPhones[agsXMPP.protocol.iq.vcard.TelephoneType.FAX] = iq.Vcard.GetTelephoneNumber(agsXMPP.protocol.iq.vcard.TelephoneType.FAX, agsXMPP.protocol.iq.vcard.TelephoneLocation.WORK);
                    _WorkPhones[agsXMPP.protocol.iq.vcard.TelephoneType.MSG] = iq.Vcard.GetTelephoneNumber(agsXMPP.protocol.iq.vcard.TelephoneType.MSG, agsXMPP.protocol.iq.vcard.TelephoneLocation.WORK);
                    _WorkAddress = iq.Vcard.GetAddress(agsXMPP.protocol.iq.vcard.AddressLocation.WORK);
                    _HomeAddress = iq.Vcard.GetAddress(agsXMPP.protocol.iq.vcard.AddressLocation.HOME);
                    _Photo = iq.Vcard.Photo;
                }
            }

            if (xmpp.IqGrabber != null) { xmpp.IqGrabber.Remove(iqID); }

            if (Retreived != null)
                Retreived(jid);

        }

        public System.Drawing.Image PrepareImageFile(string OriginalFile, int NewWidth, int MaxHeight, bool OnlyResizeIfWider)
        {
            System.Drawing.Image FullsizeImage = System.Drawing.Image.FromFile(OriginalFile);
            FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
            FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
            if (OnlyResizeIfWider)
            {
                if (FullsizeImage.Width <= NewWidth)
                    NewWidth = FullsizeImage.Width;

            }
            int NewHeight = FullsizeImage.Height * NewWidth / FullsizeImage.Width;
            if (NewHeight > MaxHeight)
            {
                NewWidth = FullsizeImage.Width * MaxHeight / FullsizeImage.Height;
                NewHeight = MaxHeight;
            }
            System.Drawing.Image NewImage = FullsizeImage.GetThumbnailImage(NewWidth, NewHeight, null, IntPtr.Zero);
            FullsizeImage.Dispose();
            return NewImage;
        }

        private string _Nickname = string.Empty;
        public string Nickname
        {
            get { return _Nickname.Trim().Substring(0, (_Nickname.Length > 255) ? 255 : _Nickname.Length ); }
            set { _Nickname = value.Trim().Substring(0, (value.Length > 255) ? 255 : value.Length); }
        }

        private string _Fullname = string.Empty;
        public string Fullname
        {
            get { return _Fullname.Trim().Substring(0, (_Fullname.Length > 255) ? 255 : _Fullname.Length); }
            set { _Fullname = value.Trim().Substring(0, (value.Length > 255) ? 255 : value.Length); }
        }

        private DateTime _Birthday = DateTime.Now;
        public DateTime Birthday
        {
            get { return _Birthday; }
            set
            { _Birthday = (value < DateTime.Now) ? value : _Birthday; }
        }

        private string _Description = string.Empty;
        public string Description
        {
            get { return _Description.Trim().Substring(0, (_Description.Length > 255) ? 255 : _Description.Length); }
            set { _Description = value.Trim().Substring(0, (value.Length > 255) ? 255 : value.Length); }
        }

        private agsXMPP.protocol.iq.vcard.Name _Name = new agsXMPP.protocol.iq.vcard.Name();
        public agsXMPP.protocol.iq.vcard.Name Name
        {
            get { return (_Name != null) ? _Name : new agsXMPP.protocol.iq.vcard.Name(); }
            set 
            {
                if (value == null)
                    _Name = new agsXMPP.protocol.iq.vcard.Name();
                else
                {
                    _Name = value;
                    _Name.Family = _Name.Family.Trim().Substring(0, (_Name.Family.Length > 255) ? 255 : _Name.Family.Length);
                    _Name.Given = _Name.Given.Trim().Substring(0, (_Name.Given.Length > 255) ? 255 : _Name.Given.Length);
                    _Name.Middle = _Name.Middle.Trim().Substring(0, (_Name.Middle.Length > 255) ? 255 : _Name.Middle.Length);
                }
            }
        }

        private agsXMPP.protocol.iq.vcard.Organization _Organization = new agsXMPP.protocol.iq.vcard.Organization();
        public agsXMPP.protocol.iq.vcard.Organization Organization
        {
            get { return (_Organization != null) ? _Organization : new agsXMPP.protocol.iq.vcard.Organization(); }
            set
            {
                if (value == null)
                    _Organization = new agsXMPP.protocol.iq.vcard.Organization();
                else
                {
                    _Organization = value;
                    _Organization.Name = _Organization.Name.Trim().Substring(0, (_Organization.Name.Length > 255) ? 255 : _Organization.Name.Length);
                    _Organization.Unit = _Organization.Unit.Trim().Substring(0, (_Organization.Unit.Length > 255) ? 255 : _Organization.Unit.Length);
                }
            }
        }

        private string _Role = string.Empty;
        public string Role
        {
            get { return _Role.Trim().Substring(0, (_Role.Length > 255) ? 255 : _Role.Length); }
            set { _Role = value.Trim().Substring(0, (value.Length > 255) ? 255 : value.Length); }
        }

        private string _Title = string.Empty;
        public string Title
        {
            get { return _Title.Trim().Substring(0, (_Title.Length > 255) ? 255 : _Title.Length); }
            set { _Title = value.Trim().Substring(0, (value.Length > 255) ? 255 : value.Length); }
        }

        private string _Url = string.Empty;
        public string Url
        {
            get { return _Url.Trim().Substring(0, (_Url.Length > 255) ? 255 : _Url.Length); }
            set { _Url = value.Trim().Substring(0, (value.Length > 255) ? 255 : value.Length); }
        }

        private agsXMPP.protocol.iq.vcard.Email _HomeEmail = null;
        public agsXMPP.protocol.iq.vcard.Email HomeEmail
        {
            get { return _HomeEmail; }
            set { _HomeEmail = value; }
        }

        private agsXMPP.protocol.iq.vcard.Email _WorkEmail = null;
        public agsXMPP.protocol.iq.vcard.Email WorkEmail
        {
            get { return _WorkEmail; }
            set { _WorkEmail = value; }
        }

        private Dictionary<agsXMPP.protocol.iq.vcard.TelephoneType, agsXMPP.protocol.iq.vcard.Telephone> _HomePhones = new Dictionary<agsXMPP.protocol.iq.vcard.TelephoneType, agsXMPP.protocol.iq.vcard.Telephone>();
        public Dictionary<agsXMPP.protocol.iq.vcard.TelephoneType, agsXMPP.protocol.iq.vcard.Telephone> HomePhones
        {
            get { return _HomePhones; }
            set { _HomePhones = value; }
        }

        private Dictionary<agsXMPP.protocol.iq.vcard.TelephoneType, agsXMPP.protocol.iq.vcard.Telephone> _WorkPhones = new Dictionary<agsXMPP.protocol.iq.vcard.TelephoneType, agsXMPP.protocol.iq.vcard.Telephone>();
        public Dictionary<agsXMPP.protocol.iq.vcard.TelephoneType, agsXMPP.protocol.iq.vcard.Telephone> WorkPhones
        {
            get { return _WorkPhones; }
            set { _WorkPhones = value; }
        }

        private agsXMPP.protocol.iq.vcard.Address _HomeAddress = null;
        public agsXMPP.protocol.iq.vcard.Address HomeAddress
        {
            get { return _HomeAddress; }
            set { _HomeAddress = value; }
        }

        private agsXMPP.protocol.iq.vcard.Address _WorkAddress = null;
        public agsXMPP.protocol.iq.vcard.Address WorkAddress
        {
            get { return _WorkAddress; }
            set { _WorkAddress = value; }
        }

        private agsXMPP.protocol.iq.vcard.Photo _Photo = null;
        public agsXMPP.protocol.iq.vcard.Photo Photo
        {
            get { return _Photo; }
            set { _Photo = value; }
        }

    }

}
